package com.qd.panda.service.stroke.delay;

import com.cdqidi.util.SpringContextHolder;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.panda.model.UserStroke;
import com.qd.panda.service.AsyncService;
import com.qd.panda.service.stroke.DisplayUtil;
import com.qd.panda.service.stroke.UserStrokeConfirmService;
import com.qd.panda.service.stroke.UserStrokeHolderService;
import com.qd.panda.service.stroke.UserStrokeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 处理超时的预约订单
 *
 * @author sjk
 * @create 2022-12-07 10:36
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserStrokeDelayService {
    private final RedissonClient redissonClient;

//    @PostConstruct
//    public void execute() {
//        RBlockingQueue<UserStrokeDelayDTO> blockingQueue = redissonClient.getBlockingQueue(UserStrokeHolderService.DELAY_QUEUE_NAME);
//        new Thread(() -> {
//            log.info("userStrokeDelay init");
//            while (true) {
//                try {
//                    if (!redissonClient.isShutdown()) {
//                        final UserStrokeDelayDTO userStrokeDelayDTO = blockingQueue.take();
//                        try {
//                            execute(userStrokeDelayDTO);
//                        } finally {
//                            userStrokeDelayDTO.freeData();
//                        }
//                    }
//                } catch (InterruptedException e) {
//                    Thread.currentThread().interrupt();
//                } catch (Exception e) {
//                    log.error("行程延时队列处理报错", e);
//                }
//            }
//        }, "userStrokeDelayThread").start();
//
//    }


    public void execute(UserStrokeDelayDTO userStrokeDelayDTO) {
        LocalDateTime now = LocalDateTime.now();
        boolean isClear = false;
        UserStrokeService userStrokeService = SpringContextHolder.getBean(UserStrokeService.class);
        UserStroke userStroke = userStrokeService.getById(userStrokeDelayDTO.getSid());
        if (null == userStroke) {
            //行程不存在的时候不处理
            return;
        }
        if (userStroke.getProcessStatus().equals(DisplayUtil.PROCESS_STATUS_0)) {
            final boolean check = SpringContextHolder.getBean(UserStrokeConfirmService.class).doorCheck(userStroke.getParkId(), userStroke.getSid(), PandaConstant.DIRECTION_1);
            log.info("parkId: {},sid:{},check: {}", userStroke.getParkId(), userStroke.getSid(), check);
            if (check) {
                //停车场设置了需要人工确认，并且用户已经扫码了，此时为true。不做处理
                return;
            }
            //只有未入场的时候才需要清理,并且发送通知消息
            UserStrokeHolderService userStrokeHolderService = SpringContextHolder.getBean(UserStrokeHolderService.class);
            userStrokeHolderService.cancelReserve(userStrokeDelayDTO.getSid(), userStrokeDelayDTO.getOrderId(), DisplayUtil.PROCESS_STATUS_2, false, "超时程序自动清理");
            isClear = true;
        }
        if (isClear) {
            final AsyncService asyncService = SpringContextHolder.getBean(AsyncService.class);
            asyncService.createNoticeMsg(userStroke, now);
        }
        log.info("开始执行超时取消预约订单操作,行程id：{}，订单ID： {},isClear: {}", userStrokeDelayDTO.getSid(), userStrokeDelayDTO.getOrderId(), isClear);
    }
}